package diko_parent.eduservice.controller;


import com.diko.commonUtils.R;
import com.diko.config.exceptionhandler.myException;
import diko_parent.eduservice.entity.Teacher;
import diko_parent.eduservice.entity.vo.teacherQuery;
import diko_parent.eduservice.service.TeacherService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
/*import org.springframework.data.domain.Page;*/

import java.util.List;

/*import java.awt.*;*/

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Emma-Li
 * @since 2020-09-11
 */
@CrossOrigin
@RestController
@RequestMapping("/eduservice/teacher")//这里最前面必须有/
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value="所有讲师列表1")
    @GetMapping("findAllTest")//这里最前面可以没有/
    public List<Teacher> findAllList(){
        List<Teacher> list=teacherService.list(null);
        return list;
    }

    //逻辑删除
   /* @ApiOperation(value="根据id删除讲师1")
    @DeleteMapping("{id}")
    public boolean removeById(@PathVariable String id){
        return teacherService.removeById(id);
    }
*/
    @ApiOperation(value="所有讲师列表2")
    @GetMapping("findAll")
    public R list(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("delete/{id}")
    public R removeById(@ApiParam(name="id",value="讲师ID",required = true)
                        @PathVariable String id){
        boolean flag=teacherService.removeById(id);
        return flag==true?R.ok():R.error();
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("pageTeacher/{page}/{limit}")
    public R pageList(@ApiParam(name="page",value = "当前页码",required = true)
                      @PathVariable long page,

                      @ApiParam(name="limit",value="每页记录数",required = true)
                      @PathVariable long limit){

        Page<Teacher> pageParam = new Page<>(page, limit);

        teacherService.page(pageParam,null);
        List<Teacher> records = pageParam.getRecords();
        long total=pageParam.getTotal();

        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "分页讲师列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name="page",value="当前页码",required = true)
            @PathVariable long page,

            @ApiParam(name="limit",value="每页记录数",required = true)
            @PathVariable long limit,

            @ApiParam(name="teacherQuery",value = "查询对象",required = false)
                    teacherQuery teacherQuery
    ){
        Page<Teacher> pageParam = new Page<>(page, limit);

        teacherService.pageQuery(pageParam,teacherQuery);
        List<Teacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("add")
    public R save(
            @ApiParam(name="teacher",value = "讲师对象",required = true)
            @RequestBody Teacher teacher
    ){
        boolean save=teacherService.save(teacher);
        return save?R.ok():R.error();
    }

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name="id",value = "讲师ID",required = true)
            @PathVariable String id
    ){
        Teacher teacher = teacherService.getById(id);
        return teacher!=null?R.ok().data("item",teacher):R.error();
    }

    @ApiOperation(value = "根据ID修改讲师")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name="id",value = "讲师ID",required = true)
            @PathVariable String id,

            @ApiParam(name="teacher",value = "讲师对象",required = true)
            @RequestBody Teacher teacher
    ){
        teacher.setId(id);
        boolean flag=teacherService.updateById(teacher);
        return flag?R.ok():R.error();
    }

    @GetMapping("test")
    public void test(){
        try {
            int a=10/0;
        }catch (Exception e){
            throw new myException(20001,"出现自定义异常");
        }
    }
}

